//
//  DrawerViewController.m
//  LO_MUSIC
//
//  Created by 向祖华 on 16/2/24.
//  Copyright © 2016年 杨少锋. All rights reserved.
//

#import "DrawerViewController.h"

@interface DrawerViewController ()<UIGestureRecognizerDelegate>

@end

@implementation DrawerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor brownColor];

    //如果没有手势，则初始化手势
    if (!self.tap) {
        //初始化手势
        self.tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
        //手势的可用属性关掉
        [self.tap setEnabled:NO];
        //设置手势代理
        self.tap.delegate = self;
        //添加手势
        [self.view addGestureRecognizer:self.tap];
    }
   
}

//设置中间视图控制器
-(void)setRootViewController:(UIViewController * )viewController
{
    //如果传进来的viewController是空的直接返回
    if (!viewController) {
        return;
    }
    //设置中间视图控制器
    [self.rootVC.view removeFromSuperview];
    self.rootVC = viewController;
    [self addChildViewController:viewController];
    [self.view addSubview:viewController.view];
    //如果手势开着，证明抽屉是打开的
    if (self.tap.enabled) {
        viewController.view.frame = CGRectMake(300, 0, 375, 667);
    }
    
    //调用（展示中间视图）关闭抽屉的方法
    [self showRootViewController:nil];
    //设置导航栏按钮
    [self setNaButton];
    
}

//展示中间视图的方法（关闭抽屉）
-(void)showRootViewController:(UIViewController *)viewController
{
    [UIView animateWithDuration:0.4 animations:^{
        self.rootVC.view.frame = CGRectMake(0, 0, 375, 667);
    }];
    
    //手势关掉
    [self.tap setEnabled:NO];
    
    //rootView交互打开
    self.rootVC.view.userInteractionEnabled = YES;
}

//打开抽屉
-(void)showLeftViewController:(UIViewController * )leftViewController
{
    
    //如果子视图集中没有左视图
    if (![self.view.subviews containsObject:self.leftVC.view]) {
        //将左视图插到最前面，即呈现抽屉
        [self.view insertSubview:self.leftVC.view atIndex:0];
    }
    //动画移动根视图控制器
    [UIView animateWithDuration:0.4 animations:^{
        self.rootVC.view.frame = CGRectMake(300, 0, 375, 667);
    }];
    //打开手势
    [self.tap setEnabled:YES];
    //阻断交互
    self.rootVC.view.userInteractionEnabled = NO;
    
    
}

//设置左上角按钮
-(void)setNaButton{
    UINavigationController * na = (UINavigationController*)self.rootVC;
    
    UIViewController * vc = na.viewControllers[0];
    
    vc.navigationItem.leftBarButtonItem = ({
        UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithTitle:@"抽屉" style:(UIBarButtonItemStyleDone) target:self action:@selector(openMenu)];
        item;
    });
    
}
//打开抽屉
-(void)openMenu
{
    
    [self showLeftViewController:nil];
    //打开手势
    [self.tap setEnabled:YES];
    //关闭rootVC视图的交互
    self.rootVC.view.userInteractionEnabled = NO;
}


-(void)tapAction
{
    //关闭抽屉
    [self showRootViewController:nil];
    //关闭手势
    [self.tap setEnabled:NO];
    //打开rootVC视图的交互
    self.rootVC.view.userInteractionEnabled = YES;
    
}

//手势的代理方法
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer == self.tap) {
        //返回的是在整个视图的框架中，属于self.rootVC.view上面的点
        return CGRectContainsPoint(self.view.frame, [gestureRecognizer locationInView:self.rootVC.view]);
    }
    return YES;
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
